import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select

class Base():
    def __init__(self,driver):
        self.driver = driver
    def open_url(self,url):
        self.driver.get(url)

    # 显示
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as p:
            times = time.strftime('%Y-%m-%d-%H:%M:%S',time.localtime())
            self.driver.save_screenshot(times+'.png')
            print(p)

    def screenshot(self):
        times = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime())
        self.driver.save_screenshot(times + '.png')

    def click(self,loc):
        self.find_element(loc).click()

    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 隐式
    def implicitly_wait(self,shijian):
        self.driver.implicitly_wait(int(shijian))

    def times(self, num):
        time.sleep(num)

    def text(self, loc):
        return self.find_element(loc).text

    def scrollTo(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    # alert弹窗处理
    # 确定
    def accept(self):
        accept_confirm = self.driver.switch_to.alert
        accept_confirm.accept()

    # 取消
    def dismiss(self):
        dismiss_cancel = self.driver.switch_to.alert
        dismiss_cancel.dismiss()

    # iframe

    # 进入框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    # 退出框架
    def content(self):
        self.driver.switch_to.defaull_content()
    # 窗口切换
    def handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
        # 鼠标事件
        # 双击
    def double_click(self, loc):
        ActionChains(self.find_element(loc)).double_click().perform()
        # 右键
    def context_click(self, loc):
        ActionChains(self.find_element(loc)).context_click().perform()
        # 长按
    def click_and_hold(self, loc):
        ActionChains(self.find_element(loc)).click_and_hold().perform()
        # 释放长按的元素
    def release_hold(self, loc):
        ActionChains(self.driver).release(self.find_element(loc)).perform()
        # 悬停
    def move_to_element(self, loc):
        element = self.find_element(loc)
        ActionChains(self.driver).move_to_element(element).perform()
        # 鼠标拖动
    def drag_and_drop(self, source_loc, target_loc):
        source = self.find_element(source_loc)  # 被拖动的元素
        target = self.find_element(target_loc)  # 目标位置元素
        ActionChains(self.driver).drag_and_drop(source, target).perform()

 # 键盘事件
    # 回车
    def enter(self):
        self.driver.find_element().send_keys(Keys.ENTER)

    # 保存
    def control_s(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'s')
    # 剪切
    def control_x(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'x')
    # 复制
    def control_c(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'c')
    # 粘贴
    def control_v(self):
        self.driver.find_element().send_keys(Keys.CONTROL,'v')
    # 浏览器窗口操作
    def refresh(self):
        self.driver.refresh()
    def close(self):
        self.driver.close()
    def back(self):
        self.driver.back()
    # 下拉框选择
    def index(self,loc,x):
        aa = Select(self.driver.find_element(loc))
        aa.select_by_index(x)
    def visible_text(self,loc,x):
        aa = Select(self.driver.find_element(loc))
        aa.deselect_by_visible_text(x)
    def value(self,loc,x):
        aa = Select(self.driver.find_element(loc))
        aa.deselect_by_value(x)



